package GUILager;

import ModelLager.InVoiceP;
import ControlLager.InVoicePCTR;
import javax.swing.JTable;
import java.util.ArrayList;

public class InvoicePanel extends javax.swing.JPanel {
    private InVoicePCTR invCTR;

    public InvoicePanel() {
        initComponents();
        invCTR = new InVoicePCTR();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableInVoice = new javax.swing.JTable();
        jButtonRydTabel = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jButtonSøgInVoice = new javax.swing.JButton();
        jButtonFindAllInVoice = new javax.swing.JButton();
        jTextFieldSøgInVoice = new javax.swing.JTextField();
        jLabelHjælpetekstTilSøgFunktion = new javax.swing.JLabel();
        jLabel2Fejlitekst = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("InVoice"));

        jTableInVoice.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "InVoice No", "Payment Date", "Amount"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTableInVoice);

        jButtonRydTabel.setText("Ryd Tabel");
        jButtonRydTabel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRydTabelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(236, Short.MAX_VALUE)
                .addComponent(jButtonRydTabel)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonRydTabel)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jButtonSøgInVoice.setText("Søg InVoice");
        jButtonSøgInVoice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSøgInVoiceActionPerformed(evt);
            }
        });

        jButtonFindAllInVoice.setText("Alle InVoice");
        jButtonFindAllInVoice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonFindAllInVoiceActionPerformed(evt);
            }
        });

        jLabelHjælpetekstTilSøgFunktion.setText("Brug InVoice No til at søge efter specifik InVoice.");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jButtonSøgInVoice)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldSøgInVoice, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabelHjælpetekstTilSøgFunktion)
                    .addComponent(jButtonFindAllInVoice))
                .addContainerGap(87, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSøgInVoice)
                    .addComponent(jTextFieldSøgInVoice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabelHjælpetekstTilSøgFunktion)
                .addGap(18, 18, 18)
                .addComponent(jButtonFindAllInVoice)
                .addContainerGap(50, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2Fejlitekst, javax.swing.GroupLayout.DEFAULT_SIZE, 336, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2Fejlitekst, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    private void clearFields()
    {
        jTextFieldSøgInVoice.setText("");
    }
    
    private void putValuesOnScreen(InVoiceP invObj)
    {
        jTextFieldSøgInVoice.setText("" + invObj.getInVoiceNo());
    }
    
    private void clearTable(JTable table)
    {
        int rowCount = 0;
        while(rowCount < table.getRowCount())
        {
            clearColumn(table, rowCount);
            rowCount++;
        }
    }
    
    private void clearColumn(JTable table, int row)
    {
        int column = 0;
        int size = table.getColumnCount();
        while(column < size)
        {
            table.setValueAt(null, row, column);
            column++;
        }
    } 
    
    private void jButtonSøgInVoiceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSøgInVoiceActionPerformed
        try
        {
            
            if(jTextFieldSøgInVoice.getText().length() == 0)
            {
               jTextFieldSøgInVoice.setText("Indtast ID!");
            }
            else
            {
                int input = Integer.parseInt(jTextFieldSøgInVoice.getText().trim());
                InVoiceP invObj = invCTR.findInVoiceP(input);
                if(invObj == null)
                {
                    jLabel2Fejlitekst.setText("InVoice No. eksisterer ikke");
                }
                else
                {
                    clearFields();
                    jTableInVoice.setValueAt(invObj.getInVoiceNo(), 0, 0);
                    jTableInVoice.setValueAt(invObj.getPaymentDate(), 0, 1);
                    jTableInVoice.setValueAt(invObj.getAmount(), 0, 2);
                    
                }
            }
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
            jLabel2Fejlitekst.setText("Indtast gyldigt InVoice No");
        }
    }//GEN-LAST:event_jButtonSøgInVoiceActionPerformed

    private void jButtonFindAllInVoiceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFindAllInVoiceActionPerformed
        clearTable(jTableInVoice);
        ArrayList<InVoiceP> inVoices = invCTR.findByAllInVoicePreQuery();
        int row = 0;
        for(InVoiceP inv : inVoices)
        {
            jTableInVoice.setValueAt(inv.getInVoiceNo(), row, 0);
            jTableInVoice.setValueAt(inv.getPaymentDate(), row, 1);
            jTableInVoice.setValueAt(inv.getAmount(), row, 2);
            row++;
        }        
    }//GEN-LAST:event_jButtonFindAllInVoiceActionPerformed

    private void jButtonRydTabelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRydTabelActionPerformed
        clearTable(jTableInVoice);
    }//GEN-LAST:event_jButtonRydTabelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonFindAllInVoice;
    private javax.swing.JButton jButtonRydTabel;
    private javax.swing.JButton jButtonSøgInVoice;
    private javax.swing.JLabel jLabel2Fejlitekst;
    private javax.swing.JLabel jLabelHjælpetekstTilSøgFunktion;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableInVoice;
    private javax.swing.JTextField jTextFieldSøgInVoice;
    // End of variables declaration//GEN-END:variables
}
